// /**
//  * Created by Administrator on 2017/3/24.
//  */
//判断上传头像是否成功
function isLoadPhoto() {

    var upload_photo = document.getElementById("upload_photo");
    var photo_name = upload_photo.value;
    var Suffix = photo_name.substring(photo_name.lastIndexOf("."));
    var arr = ['.jpg', '.gif', '.png', '.jpeg'];
    var is_exists = $.inArray(Suffix, arr);
    if (is_exists != -1) {

        $.ajaxFileUpload({
            type: "POST",
            url: "UpLoadPhoto",
            fileElementId: 'upload_photo',//文件选择框的id属性
            dataType: 'json',//服务器返回的格式
            success: function (data, status) {
                console.log(data.msg);
                if (data.msg == "success") {
                    window.alert("头像上传数据库成功");
                    return;
                } else if (data.msg == "failure") {
                    window.alert("头像上传数据库失败");
                    return;
                }
                //判断特殊语句
                var teshu = data.msg.substring(0, 2);
                if (teshu == "存在") {
                    window.alert("已存在同名照片");
                }
            },
            error: function (data, status, e) {
                window.alert(e);
            },
            //上传完成
            complete: function (date, status) {
                $('#set_up').modal('hide');
            }
        });
        return false;
    }
    console.log("上传的格式不对");
    return false;
}
//显示头像上传，设置头像的列表
function ShowSetUp() {
    $('#set_up').modal();
}
//用于删除文字 中间的空格
function world_trim(str) {
    return str.replace(/[ ]/g, "");  //去除字符算中的空格，注意这个中括号是分开的
}
function init() {
    document.onkeydown = function (event_e) {
        if (window.event) {
            event_e = window.event;
        }
        var int_keyCode = event_e.charCode || event_e.keyCode;
        if (int_keyCode == '13') {
            SendUserDate();
            return false;
        }
    }
}
//退出键操作
function outlogin() {
    $.post("", "outLogin=out", function (msg) {
        if (msg == "success") {
            window.alert("退出成功");
            redirect("../Index/index");
        } else {
            window.alert("退出失败");
        }
    });
}
//用来跳转页面
function redirect(web) {
    window.location.href = web;
}
// 发送完信息之后滚动条会在最下面
function scrollTop_end() {
    var right_top_speaking = document.getElementById('right_speaking');
    right_top_speaking.scrollTop = right_top_speaking.scrollHeight;
}
//
function SendUserDate() {
    var input_data = document.getElementById("input_box");
    var world = input_data.value.trim();
    var photo = document.getElementById("head_photo");
    var arr = new Array();
    arr[0] = input_data.value.trim();
    showInput("right", "" + photo.src, arr);
    $.get("DealData", "input_data=" + world, function (msg) {

        // var show_div = document.getElementById("right_speaking");
        // show_div.innerHTML = msg;
        // console.log(msg);
        var s = JSON.parse(msg);
        showInput("left", "/Chat_project/web/UploadImg/photo.jpg", s);
        scrollTop_end();
    });
    input_data.value = "";
}
//用来输出对话框样式
function showInput(side, photo_url, world_data) {
    // photo_url = "/Chat_project/web/Public/img/2.jpg";
    var main_div = document.createElement("DIV");
    var show_div = document.getElementById("right_speaking");
    var first_div = document.createElement("DIV");
    //显示气泡样式的div
    var bubble_div = document.createElement("DIV");
    var second_div = document.createElement("DIV");
    var third_div = document.createElement("DIV");
    var photo_img = document.createElement("IMG");
    photo_img.setAttribute("class", "media-object img-circle");
    photo_img.setAttribute("src", photo_url);
    photo_img.setAttribute("style", "width: 40px;height: 40px;");
    var first_div_class = "";
    var second_div_class = "media-body media-middle";
    var third_div_class = "";
    var bubble_div_class = "";
    if (side == "left") {
        first_div_class = "media-left media-heading";
        third_div_class = "receive_main";
        bubble_div_class = "triangle_left";
        main_div.setAttribute("class", "media");
        main_div.appendChild(first_div);
        main_div.appendChild(second_div);
        var str = "";
        console.log(world_data);
        if (world_data.error) {
            str += world_data.error;
        } else {
            if (world_data.result) {
                if( world_data.result.length == 0){
                    str += "没有数据";
                }else{
                    for (var i = 0; i < world_data.result.length; i++) {
                        str += world_data.result[i].date_time + "<br/>";
                        str += world_data.result[i].who + "<br/>";
                        str += world_data.result[i].chat_data + "<br/>";
                    }
                }
            }
            if (world_data.permission) {
                for (var i = 0; i < world_data.permission.length; i++) {
                    str += world_data.permission[i].name+ "<br/>";
                }
            }
            if(world_data.temple){
                if( world_data.temple.length == 0){
                    str += "没有数据";
                }else{
                    for (var i = 0; i < world_data.temple.length; i++) {
                        str += world_data.temple[i].user_name + "<br/>";
                        str += world_data.temple[i].speak_data + "<br/>";
                    }
                }
            }
            if (world_data.text) {
                str += world_data.text;
            }
            if(world_data.operation){
                str += world_data.operation;
            }
            if (world_data.list) {
                for (var i = 0; i < world_data.list.length; i++) {
                    str += '<br /><a href=' + world_data.list[i].detailurl + ' target="_blank">' + world_data.list[i].article + '</a>';
                }
            }
            if (world_data.url) {
                str += '<br /><a href=' + world_data.url + ' target="_blank">点此链接了解详情</a>';
            }
        }
        third_div.innerHTML = str;
    } else if (side == "right") {
        third_div.innerHTML = world_data;
        bubble_div_class = "triangle_right";
        first_div_class = "media-right media-heading";
        third_div_class = "send_main";
        main_div.setAttribute("class", "media col-lg-offset-6");
        main_div.appendChild(second_div);
        main_div.appendChild(first_div);
    }
    first_div.setAttribute("class", first_div_class);
    second_div.setAttribute("class", second_div_class);
    second_div.setAttribute("style", "padding-top: 10px;");
    third_div.setAttribute("class", third_div_class);
    bubble_div.setAttribute("class", bubble_div_class);
    first_div.appendChild(photo_img);
    second_div.appendChild(bubble_div);
    second_div.appendChild(third_div);
    show_div.appendChild(main_div);
}